package org.example.l2;

import org.junit.jupiter.api.Test;

public class TreeNode {
    int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val,
             TreeNode left,
             TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return val + "";
    }

    public static void main(String[] args) {

        String aa = "[1,2,5,3,4,null,6]";
        TreeNode node = build(aa);
    }

    public static TreeNode build(String string) {
        String s = string.substring(1, string.length() - 1);
        if (s.length() == 0) {
            return null;
        }

        String[] split = s.split(",");
        TreeNode dfs = dfs(split, 0);
        return dfs;
    }

    static TreeNode dfs(String[] split,
                 int i) {
        if (i >= split.length) {
            return null;
        }

        String a = split[i];
        if (a.equals("null")) {
            return null;
        }
        TreeNode node = new TreeNode(Integer.parseInt(a));
        int left = i *2 + 1, right = i *2 + 2;
        node.left = dfs(split, left);
        node.right = dfs(split, right);
        return node;
    }
}
